﻿using System.Windows;
using Labthink.PCSoft.UniversalHost4.UserControls.MsgBox;
using Labthink.PCSoft.UniversalHost4.Utility;

namespace Labthink.PCSoft.UniversalHost4.Instrument.Forms
{
    public partial class FrmAuth
    {
        public FrmAuth()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void BtnLogin_OnClick(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(TxtPassword.Pwd.Password) || string.IsNullOrEmpty(TxtUserId.Value)) return;
            if (TxtUserId.Value != Common.CurrentUser.LoginName)
            {
                MyMessageBox.Show(this, LanguageManager.GetLanguageValue("UserIdMustEqualsCurrent"));
                return;
            }

            var hash = PasswordHash.HashPassword(TxtPassword.Pwd.Password, Common.CurrentUser.Salt);
            if (hash != Common.CurrentUser.Password)
            {
                MyMessageBox.Show(this, LanguageManager.GetLanguageValue("AuthFailed"));
                return;
            }

            DialogResult = true;
            Close();
        }

        private void FrmAuth_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (DebugHelper.IsInDebugMode())
            {
                var up = DebugHelper.GetDebugUserPassword();
                if (up.Length > 1)
                {
                    TxtUserId.Value = up[0];
                    TxtPassword.Password = up[1];
                }
            }

            TxtUserId.Focus();
        }
    }
}
